<template>
	<view class="container">
		<!-- Top scanning section -->
		<view class="top-section">
			<view class="scan-section">
				<view class="scan-title">扫描托盘</view>
				<view class="s-input-box flex-row-between s-w">
					<input auto-focus="true" v-model="scanCode" @confirm="handleScan" class="s-input scan-input flex1"
						placeholder="请输入或扫描托盘号" />
					<image v-if="scanCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="scanCode=''"></image>
					<image class="s-input-icon s-i" src="/static/images/scan.png"></image>
				</view>
			</view>

			<!-- Order info section -->
			<view class="info-section" v-if="(erpOutBillNo || trayNo) && splitBillNo">
				<view class="info-item">
					<text class="info-label">订单号：</text>
					<text class="info-value">{{ erpOutBillNo }}</text>
				</view>
				<view class="info-item">
					<text class="info-label">托盘号：</text>
					<text class="info-value">{{ trayNo }}</text>
				</view>
			</view>
		</view>

		<!-- Materials list -->
		<scroll-view scroll-y class="materials-list" v-if="materials.length && splitBillNo">
			<view class="material-card" v-for="(item, index) in materials" :key="index">
				<view class="material-content">
					<view class="material-row">
						<text class="label">物料编号</text>
						<text class="value">{{ item.productNo }}</text>
					</view>
					<view class="material-row">
						<text class="label">物料名称</text>
						<text class="value">{{ item.productName }}</text>
					</view>
					<view class="material-row">
						<text class="label">物料批号</text>
						<text class="value">{{ item.batchNo }}</text>
					</view>
					<view class="material-row">
						<text class="label">需求数量</text>
						<text class="value">{{ item.resultQty }}</text>
					</view>
					<view class="material-row">
						<text class="label">单位</text>
						<text class="value">{{ item.cgUnitName }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="nodata-label" v-else>
			<text class="label">暂无托盘信息</text>
		</view>

		<!-- Fixed bottom area -->
		<view class="fixed-bottom">
			<!-- Bottom scanning section -->
			<view class="bottom-section">
				<view class="scan-title">扫描新托盘</view>
				<view class="s-input-box flex-row-between s-w">
					<input  v-model="newTrayCode" class="s-input scan-input flex1"
						@confirm="handleNewTrayScan" placeholder="请输入或扫描新托盘码" />
					<image v-if="newTrayCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="newTrayCode=''"></image>
					<image class="s-input-icon s-i" src="/static/images/scan.png"></image>
				</view>
			</view>

			<!-- Submit button -->
			<button class="submit-btn" @tap="handleSubmit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getSplitBill,
		getSplitBillDetailList,
		executeSplit
	} from "./api"
	export default {
		data() {
			return {
				scanCode: '',
				newTrayCode: '',
				erpOutBillNo: '',
				splitBillNo: '',
				trayNo: '',
				materials: []
			};
		},
		methods: {
			handleScan() {
				if (!this.scanCode) {
					this.$modal.msgError("请输入正确托盘号")
					return
				}
				getSplitBill({
					trayNo: this.scanCode,
					pageNum:1,
					pageSize: 999
				}).then(res => {
					if (res.code == 200) {
						if (res.total != 0) {
							this.erpOutBillNo = res.rows[0].erpOutBillNo;
							this.splitBillNo = res.rows[0].splitBillNo;
							this.trayNo = res.rows[0].trayNo;
							if (this.splitBillNo) {
								this.getDetail(this.splitBillNo);
							} else {
								this.$modal.msg("托盘" + this.trayNo + "没有对应的分拣单");
							}
						} else {
							this.trayNo = '';
							this.erpOutBillNo = '';
							this.splitBillNo = '';
							this.materials = [];
						}
					} else {
						this.trayNo = '';
						this.erpOutBillNo = '';
						this.splitBillNo = '';
						this.materials = [];
					}
				})
			},
			getDetail(billNo) {
				getSplitBillDetailList({
					orderNo: billNo,
					pageNum:1,
					pageSize: 999
				}).then(res => {
					if (res.code == 200) {
						// this.materials = res.rows;
						this.materials = res.rows.map( el=>{
							const cgPsConver = Number(el.cgPsConver);
							const ylPsConver = Number(el.ylPsConver);
							// 防止除以零的情况
							const resultQty =cgPsConver && ylPsConver ? el.qty / (cgPsConver * ylPsConver) : '';
							return{
								...el,
								resultQty: resultQty
							}
						})
					} else {
						this.initialize();
					}
				})
			},
			handleNewTrayScan() {
				// 处理新托盘扫描逻辑
			},
			handleSubmit() {
				let obj = {
					trayNo: this.scanCode,
					newTrayNo: this.newTrayCode,
					billType: 3
				};
				// 处理提交逻辑
				if (!this.scanCode) {
					this.$modal.msg("请输入托盘号");
				} else if (!this.newTrayCode) {
					this.$modal.msg("请输入新托盘号");
				} else {
					this.$modal.confirm('是否确认完成任务').then(() => {
						this.$modal.loading("加载中...")
						executeSplit(obj).then(res => {
							this.$modal.closeLoading()
							if (res.code == 200) {
								this.$modal.msgSuccess("操作成功");
								this.initialize();
							} else {
								res.msg && this.$modal.confirmClose(res.msg)
							}
						})
					})

				}
			},
			initialize() {
				this.trayNo = '';
				this.erpOutBillNo = '';
				this.splitBillNo = '';
				this.materials = [];
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #F0F2F5;
		position: relative;
		padding-bottom: 240rpx; // 为固定底部区域留出空间

		.top-section {
			padding: 30rpx 20rpx;
			background: linear-gradient(180deg, #99cfd6 0%, #4599ad 100%) !important;
		}

		.scan-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
			margin-bottom: 20rpx;

			.scan-title {
				font-size: 28rpx;
				color: #333;
				margin-bottom: 16rpx;
				font-weight: 500;
			}
		}

		.scan-input {
			background-color: #F8F9FA;
			height: 80rpx;
			padding: 0 24rpx;
			border-radius: 8rpx;
			font-size: 28rpx;
			border: 2rpx solid #E5E6EB;

			&::placeholder {
				color: #C9CDD4;
			}
		}

		.info-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.info-item {
				display: flex;
				align-items: center;
				margin-bottom: 16rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.info-label {
					font-size: 28rpx;
					color: #666;
					width: 120rpx;
				}

				.info-value {
					font-size: 28rpx;
					color: #333;
					font-weight: 500;
				}
			}
		}

		.materials-list {
			padding: 20rpx;
			max-height: calc(100vh - 800rpx);

			.material-card {
				background-color: #FFFFFF;
				border-radius: 16rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
				overflow: hidden;

				.material-header {
					padding: 20rpx 24rpx;
					background-color: #F8F9FA;
					font-size: 28rpx;
					color: #333;
					font-weight: 500;
					border-bottom: 2rpx solid #E5E6EB;
				}

				.material-content {
					padding: 24rpx;
				}

				.material-row {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 16rpx;
					font-size: 28rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.label {
						color: #666;
					}

					.value {
						color: #333;
						font-weight: 500;
					}
				}
			}
		}

		.fixed-bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: #FFFFFF;
			padding: 24rpx 20rpx;
			box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);

			.bottom-section {
				margin-bottom: 24rpx;

				.scan-title {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 16rpx;
					font-weight: 500;
				}
			}
		}

		.submit-btn {
			height: 88rpx;
			line-height: 88rpx;
			background: #4599ad;
			color: #FFFFFF;
			border-radius: 10rpx;
			font-size: 32rpx;
			text-align: center;
			font-weight: 500;
			box-shadow: 0 4rpx 12rpx rgba(46, 92, 254, 0.3);

			&:active {
				transform: scale(0.98);
			}
		}
	}

	.nodata-label {
		text-align: center;
		margin-top: 40rpx;
		color: #999999;
	}
	.s-input-box {
			position: relative;
			display: flex;
			align-items: center;
			
			.s-input {
				padding-right: 80rpx; // 为右侧图标留出空间
			}
			
			.s-input-icon {
				position: absolute;
				width: 32rpx;
				height: 32rpx;
				right: 24rpx;
				z-index: 1;
				
				&.clear-icon {
					right: 70rpx; // 清除图标位置
				}
				
				&.s-i {
					right: 24rpx; // 扫描图标位置
				}
			}
		}
</style>